<?php
require_once (APPLICATION_DIR."/library/external/XOAD/xoad.php");
require_once(APPLICATION_DIR."/library/external/fckeditor/fckeditor.php") ;
class AdminalumnimemberController extends DRX_Controller
{
	function xoadGetMeta()
	{
		XOAD_Client::mapMethods($this,array("addmember", "doactionmember", "searchmember", "editmember", "viewmember", "changestatus", "changegender"));
	}
	
	function changegender($p_arrArgs)
	{
		$objAlumni = new AlumniMember();
		if ( $objAlumni -> changeGenderMember($p_arrArgs) )
			return "SUCCESS";
		else
			return "FAIL";
	}
	
	function changestatus($p_arrArgs)
	{
		$objAlumni = new AlumniMember();
		if ( $objAlumni -> changeStatusMember($p_arrArgs) )
			return "SUCCESS";
		else
			return "FAIL";
	}
	
	function viewmember($p_arrArgs)
	{
		$objSmarty = DRX::load('smarty');
		//$objAlumni = new AlumniMember();
		$objAlumni = new Alumni();
		$objGeneral = new AlumniGeneral();
		
		if ( !$arrMemberPersonal = $objAlumni -> getInfoMemberId($p_arrArgs['mb_id']) )
			return 0;
		
		$arrMemberContact = $objAlumni -> getInfoMemberContact($p_arrArgs['mb_id']);
		$arrMemberCurrentPosition = $objAlumni -> getListCurrentPosition($p_arrArgs['mb_id']);
		$arrMemberPastPosition = $objAlumni -> getListPastPosition($p_arrArgs['mb_id']);
		
		$objSmarty -> assign("arrMemberPersonal", $arrMemberPersonal);
		$objSmarty -> assign("arrMemberContact", $arrMemberContact);
		$objSmarty -> assign("arrMemberCurrentPosition", $arrMemberCurrentPosition);
		$objSmarty -> assign("arrMemberPastPosition", $arrMemberPastPosition);
		
		XOAD_HTML::getElementById($p_arrArgs['id']) -> innerHTML = $objSmarty -> fetch("admin/alumni/member/frm_view_member.tpl");
	}
	
	function deletemember($p_arrArgs)
	{
		$objAlumni = new AlumniMember();
		if ( $objAlumni -> deleteMember($p_arrArgs['mb_id']) )
			return "SUCCESS";
		else
			return "FAIL";
	}
	
	function editmember($p_arrArgs)
	{
		$objSmarty = DRX::load('smarty');
		$objAlumni = new AlumniMember();
		
		if ( !$arrInfoMember = $objAlumni -> getInfoMemberId($p_arrArgs['mb_id']) )
			return 0;
		
		$objSmarty -> assign("arrIntake", $objAlumni -> getListIntake());
		$objSmarty -> assign("arrInfoMember", $arrInfoMember);
		
		XOAD_HTML::getElementById($p_arrArgs['id']) -> innerHTML = $objSmarty -> fetch("admin/alumni/member/frm_edit_member.tpl");
	}
	
	function searchmember($p_arrArgs)
	{	
		$objAlumni = new AlumniMember();
		$objSmarty = DRX::load('smarty');
		$arrForm = $p_arrArgs['form'];
		
		define("RESULTS_PER_PAGE_MEMBER", 20);
		$arrForm['start'] = ($arrForm['page']-1)*RESULTS_PER_PAGE_MEMBER;
		$arrForm['limit'] = RESULTS_PER_PAGE_MEMBER;
		
		$objListMember = $objAlumni -> searchMember($arrForm);
		
		//phan trang
		include "IO_Page.php";
		$intTotal= $objListMember -> count('distinct');
		$objNaviPage = new IO_Page(5, RESULTS_PER_PAGE_MEMBER, 'onPage');
		$strNaviPage = $objNaviPage -> createPageNavigator($arrForm['page'], $intTotal, "");		
		$objSmarty -> assign("NAVIGATION_PAGE", $strNaviPage);
		$objSmarty -> assign("TOTAL", $intTotal);
		$objSmarty -> assign("objListMember", $objListMember);
		$objSmarty -> assign("intStart", $arrForm['start']);
		
		XOAD_HTML::getElementById($p_arrArgs['id']) -> innerHTML = $objSmarty -> fetch("admin/alumni/member/list_search_member.tpl");
	}
	
	function doactionmember($p_arrArgs)
	{
		$objAlumni = new AlumniMember();
		$arrForm = $p_arrArgs['form'];
		
		if ( $arrForm['action'] == "add" )
		{
			//check xem da ton tai usernam nay trong hethong hay chua
			if ( $objAlumni -> getInfoMemberUsername( trim($arrForm['username']) ) )
				return "EXIST";
			if ( $objAlumni -> addMember($arrForm) ) 
			{
				$objForum = new AlumniController();
				$arrForm['private_email'] = '';
				$objForum -> addMemberForum($arrForm);
				return "SUCCESS";
			}
			else
				return "FAIL";
		}
		else if ( $arrForm["action"] == "update" )
		{
			if ( $objAlumni -> updateMember($arrForm) ) 
			{
				if ( trim($arrForm['password']) != '' )
				{
					$objForum = new AlumniController();
					$arrInfoMember = $objAlumni -> getInfoMemberId($arrForm['id']);
					$objForum -> changePasswordOfForum($arrInfoMember['username'], $arrForm['password']);
				}
				return "SUCCESS";
			}
			else
				return "FAIL";
		}
	}
	
	function addmember($p_arrArgs)
	{
		$objSmarty = DRX::load('smarty');
		$objAlumni = new AlumniMember();
		$objSmarty -> assign("arrIntake", $objAlumni -> getListIntake());
		
		XOAD_HTML::getElementById($p_arrArgs['id']) -> innerHTML = $objSmarty -> fetch("admin/alumni/member/frm_add_member.tpl");
	}
	
	function indexAction()
	{
		//$this -> listAction();
		$objSmarty = DRX::load('smarty');
		$this -> runXoadServer();
		$this -> pathWeb();
		$this -> getLeftMenu();
		$objSmarty -> assign("content", $objSmarty -> fetch("admin/alumni/member/index.tpl"));
		$objSmarty->display("admin/admin.tpl");
	}
	
	function getLeftMenu()
	{
		$objSmarty = DRX::load('smarty');
		$objAlumni = new AlumniMember();
		
		$arrIntake = $objAlumni -> getListIntake();
		
		$objSmarty -> assign("arrIntake", $arrIntake);		
		$objSmarty -> assign("leftmenu", $objSmarty -> fetch("admin/alumni/member/left_menu.tpl"));
	}
	
	function createFCK($p_strName, $p_strValue = "")
	{
		$oFCKeditor = new FCKeditor($p_strName) ;
		$oFCKeditor->BasePath = FCK_BASE_PATH;
		$oFCKeditor->Height = FCK_DEFAULT_HEIGHT;
		$oFCKeditor->Value = $p_strValue;
		$fck = $oFCKeditor->Create();
		return $fck;
	}
	
	function pathWeb()
	{
		$objSmarty = DRX::load('smarty');
		$list_path = array();
		$prefix_url             = APPLICATION_URL . ADMIN_URL;        
    $list_path[0]['url']    = $prefix_url;
    $list_path[0]['title']  = ADMIN_HOME_PATH_WEB_TITLE;
		$last_path['url']    = $prefix_url . "/alumni/member";
    $last_path['title']  = "Alumni Member";
		
		$objSmarty -> assign("list_path",$list_path);
		$objSmarty -> assign("last_path",$last_path);
		$path_web = $objSmarty->fetch("admin/path_web.tpl");
	  $objSmarty -> assign("path_web",$path_web);
	}
	
	function runXoadServer()
	{
		$objSmarty = DRX::load('smarty');
		
		if ( XOAD_Server::runServer() )
		{
			exit();
		}
		
		$objSmarty -> assign("xoad_header", XOAD_Utilities::header(APPLICATION_URL . "library/external/XOAD"));	
		$objSmarty -> assign("xoad_handler", XOAD_Client::register($this, "/Adminalumnimember"));
	}	
}
?>